//
//  mapViewController.m
//  fugitiveVertical
//
//  Created by Eugene Bistolas on 5/10/11.
//  Copyright 2011 __MyCompanyName__. All rights reserved.
//

#import "mapViewController.h"


@implementation mapViewController
@synthesize eng = eng_; 
@synthesize appDelegate = appDelegate_; 

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
        self.title = @"Maps!";
    }

    return self;
}

- (void)dealloc
{
    [self.appDelegate removeObserver:self forKeyPath:@"currentLocation"]; 

    [super dealloc];
    [mapView release];
    [seng release]; 
    [locateButton release];
    [powerupButton release];
}

- (void)didReceiveMemoryWarning
{
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
    
    // Release any cached data, images, etc that aren't in use.
}

#pragma mark - View lifecycle

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.

}

- (void)viewDidUnload
{
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
    [mapView release];
    mapView = nil;
    [locateButton release];
    locateButton = nil;
    [powerupButton release];
    powerupButton = nil;
    [seng release];
    seng = nil;
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    // Return YES for supported orientations
    return interfaceOrientation == UIInterfaceOrientationPortrait;
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewDidAppear:animated];
    [self configMap];      
}

- (void) configMap {
    
    self.appDelegate = (fugitiveVerticalAppDelegate*)[[UIApplication sharedApplication] delegate]; 
    //Get server data and parse
    seng = [[ServerEngine alloc] init]; 
    [seng parseServerData]; 
    
    [self.appDelegate addObserver:self forKeyPath:@"currentLocation" options:(NSKeyValueObservingOptionNew | NSKeyValueObservingOptionInitial) context:nil]; 
    UIImage *img = [[UIImage imageNamed:@"UIPopupAlertSheetButton@2x.png"] stretchableImageWithLeftCapWidth:12 topCapHeight:0];
    
    //set up buttons
    [locateButton setBackgroundImage:img forState:UIControlStateNormal]; 
    [locateButton setBackgroundImage:img forState:UIControlStateHighlighted];
    [locateButton setBackgroundImage:img forState:UIControlStateSelected];
    [powerupButton setBackgroundImage:img forState:UIControlStateNormal]; 
    [powerupButton setBackgroundImage:img forState:UIControlStateHighlighted];
    [powerupButton setBackgroundImage:img forState:UIControlStateSelected];
    
    NSLog(@"Map view loaded\n");
    //Set initial zoom. 
    MKCoordinateSpan span = MKCoordinateSpanMake(0.01, 0.02);
    userRegion  = MKCoordinateRegionMake(self.appDelegate.currentLocation.coordinate, span); 


}

- (MKAnnotationView *) mapView:(MKMapView *) mapView viewForAnnotation:(id ) annotation {
    
	MKPinAnnotationView *customAnnotationView = [[MKAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:@"pin"];
    customAnnotationView.pinColor = MKPinAnnotationColorGreen;
    customAnnotationView.animatesDrop = YES; 
	return customAnnotationView;
}


- (void) observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context {
    if ([keyPath isEqualToString:@"currentLocation"]) {
        MKCoordinateSpan span = MKCoordinateSpanMake(0.01, 0.02);
        userRegion  = MKCoordinateRegionMake(self.appDelegate.currentLocation.coordinate, span); 
        
        //Add logic in here to record gps coordinate. 
    }

}

- (IBAction) buttonPressed:(UIButton *)sender {
    if (sender.tag == 1) {
        if (locateUser == NO) {
            locateUser = YES;
            [mapView setRegion:userRegion animated:YES]; 
            [mapView regionThatFits:userRegion]; 
            [mapView setShowsUserLocation:YES];
        }
        else {
            locateUser = NO; 
            [mapView setShowsUserLocation:NO]; 
            
        }
    }
    if (sender.tag == 2) {
        for (Powerup *p in seng.powerups) {
            [mapView addAnnotation:p]; 
            //[p release]; 
        }
    }

}

@end
